package com.marketingcenter.controller;

import com.marketingcenter.entity.PackageCard;
import com.marketingcenter.service.PackageCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐卡控制器
 */
@RestController
@RequestMapping("/package-cards")
public class PackageCardController {

    @Autowired
    private PackageCardService packageCardService;

    /**
     * 创建套餐卡
     */
    @PostMapping
    public ResponseEntity<PackageCard> create(@RequestBody PackageCard card) {
        card.setType(PackageCard.PromotionType.PACKAGE_CARD);
        PackageCard createdCard = packageCardService.create(card);
        return ResponseEntity.ok(createdCard);
    }

    /**
     * 更新套餐卡
     */
    @PutMapping("/{id}")
    public ResponseEntity<PackageCard> update(@PathVariable Long id, @RequestBody PackageCard card) {
        card.setId(id);
        PackageCard updatedCard = packageCardService.update(card);
        return ResponseEntity.ok(updatedCard);
    }

    /**
     * 删除套餐卡
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        packageCardService.delete(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 根据ID查询套餐卡
     */
    @GetMapping("/{id}")
    public ResponseEntity<PackageCard> getById(@PathVariable Long id) {
        PackageCard card = packageCardService.findById(id);
        return card != null ? ResponseEntity.ok(card) : ResponseEntity.notFound().build();
    }

    /**
     * 查询所有套餐卡
     */
    @GetMapping
    public ResponseEntity<List<PackageCard>> getAll() {
        List<PackageCard> cards = packageCardService.findAll();
        return ResponseEntity.ok(cards);
    }

    /**
     * 根据状态查询套餐卡
     */
    @GetMapping("/status/{status}")
    public ResponseEntity<List<PackageCard>> getByStatus(@PathVariable Integer status) {
        List<PackageCard> cards = packageCardService.findByStatus(status);
        return ResponseEntity.ok(cards);
    }

    /**
     * 查询活动中的套餐卡
     */
    @GetMapping("/active")
    public ResponseEntity<List<PackageCard>> getActive() {
        List<PackageCard> cards = packageCardService.findActivePromotions();
        return ResponseEntity.ok(cards);
    }

    /**
     * 启用套餐卡
     */
    @PutMapping("/{id}/enable")
    public ResponseEntity<Void> enable(@PathVariable Long id) {
        packageCardService.enable(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 停用套餐卡
     */
    @PutMapping("/{id}/disable")
    public ResponseEntity<Void> disable(@PathVariable Long id) {
        packageCardService.disable(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 用户购买套餐卡
     */
    @PostMapping("/{id}/purchase")
    public ResponseEntity<Boolean> purchase(@PathVariable Long id, @RequestParam Long userId) {
        boolean success = packageCardService.purchasePackageCard(id, userId);
        return ResponseEntity.ok(success);
    }

    /**
     * 查询套餐卡的折扣力度
     */
    @GetMapping("/{id}/discount-rate")
    public ResponseEntity<Double> getDiscountRate(@PathVariable Long id) {
        double discountRate = packageCardService.getDiscountRate(id);
        return ResponseEntity.ok(discountRate);
    }

}